package com.ruoyi.lwpq.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.lwpq.domain.LwpqYggl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lwpq.mapper.LwpqLwpqMapper;
import com.ruoyi.lwpq.domain.LwpqLwpq;
import com.ruoyi.lwpq.service.ILwpqLwpqService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

/**
 * 劳务派遣Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-10
 */
@Service
public class LwpqLwpqServiceImpl implements ILwpqLwpqService 
{
    @Autowired
    private LwpqLwpqMapper lwpqLwpqMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询劳务派遣
     * 
     * @param id 劳务派遣主键
     * @return 劳务派遣
     */
    @Override
    public LwpqLwpq selectLwpqLwpqById(Long id)
    {
        return lwpqLwpqMapper.selectLwpqLwpqById(id);
    }

    /**
     * 查询劳务派遣列表
     * 
     * @param lwpqLwpq 劳务派遣
     * @return 劳务派遣
     */
    @Override
    public List<LwpqLwpq> selectLwpqLwpqList(LwpqLwpq lwpqLwpq)
    {
        return lwpqLwpqMapper.selectLwpqLwpqList(lwpqLwpq);
    }

    /**
     * 新增劳务派遣
     * 
     * @param lwpqLwpq 劳务派遣
     * @return 结果
     */
    @Override
    public int insertLwpqLwpq(LwpqLwpq lwpqLwpq)
    {
        lwpqLwpq.setCreateTime(DateUtils.getNowDate());
        return lwpqLwpqMapper.insertLwpqLwpq(lwpqLwpq);
    }

    /**
     * 修改劳务派遣
     * 
     * @param lwpqLwpq 劳务派遣
     * @return 结果
     */
    @Override
    public int updateLwpqLwpq(LwpqLwpq lwpqLwpq)
    {
        lwpqLwpq.setUpdateTime(DateUtils.getNowDate());
        return lwpqLwpqMapper.updateLwpqLwpq(lwpqLwpq);
    }

    /**
     * 批量删除劳务派遣
     * 
     * @param ids 需要删除的劳务派遣主键
     * @return 结果
     */
    @Override
    public int deleteLwpqLwpqByIds(String ids)
    {
        return lwpqLwpqMapper.deleteLwpqLwpqByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除劳务派遣信息
     * 
     * @param id 劳务派遣主键
     * @return 结果
     */
    @Override
    public int deleteLwpqLwpqById(Long id)
    {
        return lwpqLwpqMapper.deleteLwpqLwpqById(id);
    }

    @Override
    public String importLwpq(List<LwpqLwpq> lwpq, boolean updateSupport, String loginName) {
        if (StringUtils.isNull(lwpq) || lwpq.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (LwpqLwpq lw : lwpq)
        {
            try
            {
                // 验证是否存在这个用户

                BeanValidators.validateWithException(validator, lw);
                lw.setCreateBy(loginName);
                this.insertLwpqLwpq(lw);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + lw.getName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + lw.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
